<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取变量的字符串值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.settype.html">« settype</a></li>
      <li style="float: right;"><a href="function.unserialize.html">unserialize »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.var.html">Variable handling 函数</a></li>
    <li>获取变量的字符串值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.strval" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strval</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strval</span> &mdash; <span class="dc-title">获取变量的字符串值</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.strval-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strval</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type">string</span></div>

  <p class="simpara">
   返回变量的字符串值。参见 <span class="type">string</span> 文档获取更多关于转换为字符串的信息。
  </p>
  <p class="simpara">
   此函数对返回值不执行任何格式设置。如果需要将数值格式化字符串的方法，请参阅
   <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span> 和 <span class="function"><a href="function.number-format.html" class="function">number_format()</a></span>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strval-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
       可以转换为 <span class="type">string</span> 的变量。
      </p>
      <p class="para">
       <code class="parameter">value</code> 可以是任何标量类型或者实现了
       <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a>
       方法的对象。不能在数组或者未实现 <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a>
       方法的对象上使用 <span class="function"><strong>strval()</strong></span>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.strval-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <code class="parameter">value</code> 的 <span class="type">string</span> 值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strval-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5302">
    <p><strong>示例 #1 
     使用 PHP 魔术方法 <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a>
     的 <span class="function"><strong>strval()</strong></span> 示例。
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">StrValTest<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;打印“StrValTest”<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">strval</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">StrValTest</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.strval-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.boolval.html" class="function" rel="rdfs-seeAlso">boolval()</a> - 获取变量的布尔值</span></li>
    <li class="member"><span class="function"><a href="function.floatval.html" class="function" rel="rdfs-seeAlso">floatval()</a> - 获取变量的浮点值</span></li>
    <li class="member"><span class="function"><a href="function.intval.html" class="function" rel="rdfs-seeAlso">intval()</a> - 获取变量的整数值</span></li>
    <li class="member"><span class="function"><a href="function.settype.html" class="function" rel="rdfs-seeAlso">settype()</a> - 设置变量的类型</span></li>
    <li class="member"><span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - 返回格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
    <li class="member"><a href="language.types.type-juggling.html" class="link">Type juggling</a></li>
    <li class="member"><a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>